package com.cloudpolice.door.controller;

import com.cloudpolice.door.model.EquipParaModel;
import com.cloudpolice.door.model.InPutParam.EquipParaInsertInParam;
import com.cloudpolice.door.myenum.ParamEnum;
import com.cloudpolice.door.service.EquipParaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.model.ResultVOPage;
import lh.toolclass.LhClass;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：梁昊，后端工程师：flyman5，前端工程师：孙耀
 * @create 2020-02-27 14:50
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/equipParaController")
@Api(value = "硬件设备聚合层", description = "门禁机等硬件设备控制层")
public class EquipParaController {
    @Autowired
    EquipParaService equipParaService;

    /**
     * 重置指定设备所有参数
     *
     * @param sn 设备SN
     * @return 影响条数
     */
    @ApiOperation(value = "重置指定设备所有参数", notes = "影响条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", required = true)
    })
    @PostMapping("/insertAllEquipParam")
    public ResultVO insertAllEquipParam(@RequestParam(value = "useId") String sn) {
        final int updateCount = equipParaService.insertAllEquipParam(sn);
        if (updateCount > 0)
            return ResultStruct.success(String.format("重置%d个参数", updateCount));
        else
            return ResultStruct.error("重置失败", ResultVO.class, int.class);
    }

    /**
     * 删除设备全部参数，方法ID：DE20200227153502112471FF8EA7E95
     *
     * @param sn 设备SN
     * @return 影响条数
     */
    @ApiOperation(value = "删除设备全部参数,方法ID：DE20200227153502112471FF8EA7E95", notes = "影响条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", required = true)
    })
    @PostMapping("/deleteEquipAllParamBySn")
    public ResultVO deleteEquipAllParamBySn(@RequestParam(value = "useId") String sn) {
        return deleteEquipParamBySnAndId(null, sn);
    }

    private ResultVO deleteEquipParamBySnAndId(ParamEnum paramEnum, String sn) {
        sn = sn == null ? null : sn.trim();
        EquipParaInsertInParam equipParaInsertInParam = new EquipParaInsertInParam();
        equipParaInsertInParam.setSn(sn);
        if (paramEnum != null)
            equipParaInsertInParam.setId(paramEnum.getParamId());
        int updateCount = equipParaService.deleteEquipParamBySnAndId(equipParaInsertInParam);
        if (updateCount > 0)
            return ResultStruct.success(updateCount);
        else
            return ResultStruct.error("删除失败", ResultVO.class, int.class);
    }

    /**
     * 加入设备门禁机管理员密码参数，方法ID：IN2020022714591020259724C215A3A
     *
     * @param sn    设备SN
     * @param param 参数
     * @return 影响条数
     */
    @ApiOperation(value = "加入设备门禁机管理员密码参数,方法ID：IN2020022714591020259724C215A3A", notes = "设备参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", required = true)
            , @ApiImplicitParam(name = "param", value = "参数", dataType = "String", required = true)
    })
    @PostMapping("/insertOrUpdateEquipParamPASSWORD")
    public ResultVO insertOrUpdateEquipParamPASSWORD(@RequestParam(value = "useId") String sn
            , @RequestParam(value = "param") String param) {
        final int i = insertEquipParam(ParamEnum.PASS_WORD, sn, param);
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error("没有增加成功", ResultVO.class);
    }

    /**
     * 加入设备门禁机每页下载条数参数，方法ID：IN2020022714591020259724C215A3A
     *
     * @param sn    设备SN
     * @param param 参数
     * @return 影响条数
     */
    @ApiOperation(value = "加入设备门禁机每页下载条数参数,方法ID：IN2020022714591020259724C215A3A", notes = "设备参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", required = true)
            , @ApiImplicitParam(name = "param", value = "参数", dataType = "String", required = true)
    })
    @PostMapping("/insertOrUpdateEquipParamPAGESIZE")
    public ResultVO insertOrUpdateEquipParamPAGESIZE(@RequestParam(value = "useId") String sn
            , @RequestParam(value = "param") String param) {
        final int i = insertEquipParam(ParamEnum.PAGE_SIZE, sn, param);
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error("没有增加成功", ResultVO.class);
    }

    /**
     * 加入设备门禁机外部接口URL参数，方法ID：IN2020022714591020259724C215A3A
     *
     * @param sn    设备SN
     * @param param 参数
     * @return 影响条数
     */
    @ApiOperation(value = "加入设备门禁机外部接口URL参数,方法ID：IN2020022714591020259724C215A3A", notes = "设备参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", required = true)
            , @ApiImplicitParam(name = "param", value = "参数", dataType = "String", required = true)
    })
    @PostMapping("/insertOrUpdateEquipParamURLADDRESSFRONT")
    public ResultVO insertOrUpdateEquipParamURLADDRESSFRONT(@RequestParam(value = "useId") String sn
            , @RequestParam(value = "param") String param) {
        final int i = insertEquipParam(ParamEnum.URL_ADDRESS_FRONT, sn, param);
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error("没有增加成功", ResultVO.class);
    }

    /**
     * 加入设备门禁机本地AI算法名称参数，方法ID：IN2020022714591020259724C215A3A
     *
     * @param sn    设备SN
     * @param param 参数
     * @return 影响条数
     */
    @ApiOperation(value = "加入设备门禁机本地AI算法名称参数,方法ID：IN2020022714591020259724C215A3A", notes = "设备参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", required = true)
            , @ApiImplicitParam(name = "param", value = "参数", dataType = "String", required = true)
    })
    @PostMapping("/insertOrUpdateEquipParamAINAME")
    public ResultVO insertOrUpdateEquipParamAINAME(@RequestParam(value = "useId") String sn
            , @RequestParam(value = "param") String param) {
        final int i = insertEquipParam(ParamEnum.AI_NAME, sn, param);
        if (i > 0)
            return ResultStruct.success(i);
        else
            return ResultStruct.error("没有增加成功", ResultVO.class);
    }

    private int insertEquipParam(ParamEnum paramEnum
            , String sn
            , String param) {
        param = param == null ? param : param.trim();
        sn = sn == null ? sn : sn.trim();

        EquipParaInsertInParam equipParaInsertInParam = new EquipParaInsertInParam();
        String mainKey = paramEnum.getParamId();//这里引用架包中的生成主键方法
        equipParaInsertInParam.setId(mainKey);
        equipParaInsertInParam.setSn(sn);
        equipParaInsertInParam.setParam(param);
        equipParaInsertInParam.setRemark(paramEnum.getRemark());
        int resultCount = equipParaService.insertEquipParam(equipParaInsertInParam);

        return resultCount;
    }


    /**
     * 根据设备SN号，得到设备参数，方法ID：SE20200227144951987C9B49A90F738
     *
     * @param sn    表inf_EquipmentParam,字段名sn:设备SN
     * @param page  当前页数
     * @param limit 每页条数
     * @return 设备参数列表
     */
    @ApiOperation(value = "根据设备SN号，得到设备参数,方法ID：SE20200227144951987C9B49A90F738", notes = "设备参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "useId", value = "设备SN", dataType = "String", required = true),
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "String")
    })
    @PostMapping("/selectEquipParamsBySn")
    public ResultVOPage selectEquipParamsBySn(@RequestParam(value = "useId") String sn
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) {
        sn = sn == null ? sn : sn.trim();

        PageHelper.startPage(page, limit);
        List<EquipParaModel> equipParaModels = equipParaService.selectEquipParamsBySn(sn);
        PageInfo pageInfo = new PageInfo<>(equipParaModels, limit);
        System.out.println(LhClass.getVersion());
        return ResultStruct.successPage(equipParaModels, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }
}
